home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / sound / convert.lzh / ST2SPARC.C < prev   
C/C++ Source or Header  |  1990-09-29  |  5KB  |  177 lines

  1. /* 
  2.     NOTE:    Since standard Atari ST samples are in the same format as
  3.         Mac samples, this program will convert Atari ST samples
  4.         to Sparcstation uLAW format as well.
  5. */
  6.  
  7. /************************************************************************/
  8. /*      Copyright 1989 by Rich Gopstein and Harris Corporation          */
  9. /*                                                                      */
  10. /*      Permission to use, copy, modify, and distribute this software   */
  11. /*      and its documentation for any purpose and without fee is        */
  12. /*      hereby granted, provided that the above copyright notice        */
  13. /*      appears in all copies and that both that copyright notice and   */
  14. /*      this permission notice appear in supporting documentation, and  */
  15. /*      that the name of Rich Gopstein and Harris Corporation not be    */
  16. /*      used in advertising or publicity pertaining to distribution     */
  17. /*      of the software without specific, written prior permission.     */
  18. /*      Rich Gopstein and Harris Corporation make no representations    */
  19. /*      about the suitability of this software for any purpose.  It     */
  20. /*      provided "as is" without express or implied warranty.           */
  21. /************************************************************************/
  22.  
  23. /************************************************************************/
  24. /* sound2sun.c - Convert sampled audio files into uLAW format for the   */
  25. /*               Sparcstation 1.                                        */
  26. /*               Send comments to ..!rutgers!soleil!gopstein            */
  27. /************************************************************************/
  28. /*                                    */
  29. /*  Modified November 27, 1989 to convert to 8000 samples/sec           */
  30. /*   (contrary to man page)                                             */
  31. /*                                    */
  32. /*  Fixed Bug with converting slow sample speeds            */
  33. /*                                    */
  34. /************************************************************************/
  35.  
  36.  
  37. #include <stdio.h>
  38.  
  39. #define DEFAULT_FREQUENCY 11000
  40.  
  41. FILE *infile, *outfile;
  42.  
  43. /* convert two's complement ch into uLAW format */
  44.  
  45. unsigned int cvt(ch)
  46. int ch;
  47. {
  48.  
  49.   int mask;
  50.  
  51.   if (ch < 0) {
  52.     ch = -ch;
  53.     mask = 0x7f;
  54.   } else {
  55.     mask = 0xff;
  56.   }
  57.  
  58.   if (ch < 32) {
  59.     ch = 0xF0 | 15 - (ch / 2);
  60.   } else if (ch < 96) {
  61.     ch = 0xE0 | 15 - (ch - 32) / 4;
  62.   } else if (ch < 224) {
  63.     ch = 0xD0 | 15 - (ch - 96) / 8;
  64.   } else if (ch < 480) {
  65.     ch = 0xC0 | 15 - (ch - 224) / 16;
  66.   } else if (ch < 992) {
  67.     ch = 0xB0 | 15 - (ch - 480) / 32;
  68.   } else if (ch < 2016) {
  69.     ch = 0xA0 | 15 - (ch - 992) / 64;
  70.   } else if (ch < 4064) {
  71.     ch = 0x90 | 15 - (ch - 2016) / 128;
  72.   } else if (ch < 8160) {
  73.     ch = 0x80 | 15 - (ch - 4064) /  256;
  74.   } else {
  75.     ch = 0x80;
  76.   }
  77. return (mask & ch);
  78. }
  79.       
  80. /*******************************************************
  81. /*                                                     */
  82. /* Usage is "sound2sun [-f frequency] infile outfile"  */
  83. /*                                                     */
  84. /* "frequency" is the samples per second of the infile */
  85. /* the outfile is always 8000 samples per second.      */
  86. /*                                                     */
  87. /*******************************************************/
  88.  
  89. /***********************************************************************/
  90. /*                                                                     */
  91. /* The input file is expected to be a stream of one-byte excess-128    */
  92. /* samples.  Each sample is converted to 2's complement by subtracting */
  93. /* 128, then converted to uLAW and output.  We calculate the proper    */
  94. /* number of input bytes to skip in order to make the sample frequency */
  95. /* convert to 8000/sec properly.  Interpolation could be added, but it */
  96. /* doesn't appear to be necessary.                                     */
  97. /*                                                                     */
  98. /***********************************************************************/
  99.  
  100.  
  101. main(argc, argv)
  102. int argc;
  103. char *argv[];
  104. {
  105.  
  106.   float sum = 0;
  107.   float frequency, increment;
  108.  
  109.   unsigned char ch;
  110.   unsigned char ulaw;
  111.  
  112.   int chr;
  113.  
  114.   if ((argc != 3) && (argc != 5)) {
  115.     fprintf(stderr,"Usage: sound2sun [-f frequency] infile outfile\n");
  116.     exit(1);
  117.   }
  118.  
  119.   if (argc == 5) {
  120.     if (strcmp(argv[1], "-f") != 0) {
  121.       fprintf(stderr, "Usage: sound2sun [-f frequency] infile outfile\n");
  122.       exit(1);
  123.     } else {
  124.       frequency = atoi(argv[2]);
  125.     }
  126.   } else {
  127.     frequency = DEFAULT_FREQUENCY;
  128.   }
  129.  
  130.   if ((infile = fopen(argv[argc-2], "r")) == NULL) {
  131.     perror("Error opening infile");
  132.     exit(0);
  133.   }
  134.  
  135.   if ((outfile = fopen(argv[argc-1], "w")) == NULL) {
  136.     perror("Error opening outfile");
  137.     exit(0);
  138.   }
  139.  
  140.   /* increment is the number of bytes to read each time */
  141.  
  142.   increment = frequency / 8000;
  143.  
  144.   ch = fgetc(infile);
  145.  
  146.   while (!feof(infile)) {
  147.  
  148.     /* convert the excess 128 to two's complement */
  149.  
  150.     chr = 0x80 - ch;
  151.  
  152.     /* increase the volume */
  153.     /* convert to uLAW */
  154.  
  155.     ulaw = cvt(chr * 16);
  156.  
  157.     /* output it */
  158.  
  159.     fputc((char) ulaw, outfile);
  160.  
  161.     /* skip enough input bytes to compensate for sampling frequency diff */
  162.  
  163.     sum += increment;
  164.  
  165.     while(sum > 0) {
  166.       if (!feof(infile)) ch = fgetc(infile);
  167.       sum--;
  168.     }
  169.  
  170.   }
  171.  
  172.   fclose(infile);
  173.   fclose(outfile);
  174. }
  175.  
  176.  
  177.